package yawning.mew.cat;

import yawning.mew.ProcEffect.Trigger;
import yawning.mew.cat.CatModelSimState.CatTargetData;
import yawning.mew.character.CharacterModel.AttackResult;
import yawning.mew.sim.SimulationAction;
import yawning.mew.sim.SimulationEventAction;
import yawning.mew.sim.SimulationTarget;

public class EventPounce extends SimulationEventAction {
	CatModelSimState mState;
	private SimulationTarget mTarget;
	private CatTargetData mTargetData;
	private String mTargetName;
	private double mAttackPower;
	private double mPCrit;
	private double mMasteryMult;
	private double mToTTMult;
	boolean mTigersFury;

	EventPounce(CatModelSimState state, CatTargetData targetData) {
		super(state.mSimEngine);
		mState = state;
		mTarget = targetData.getSimulationTarget();
		mTargetData = targetData;
		mTargetName = mTarget.getTarget().getName();
	}

	@Override
	public double perform() {
		int cost = mState.getEnergyCost(mState.mModel.pounceCost);
		if (mState.getEnergy() < cost && !mState.mOocEv.isUp()) return SimulationAction.ACTION_FAIL;
		if (mState.isInCombat()) return SimulationAction.ACTION_FAIL; // Can't use this mid combat at all.

		int prevEnergy = mState.getEnergy(); int prevCps = mState.getComboPoints();

		AttackResult result = mState.mModel.getYellowStealthResult(0.0);

		if (result == AttackResult.MISS) {
			cost = (int) Math.round(cost * 0.2);
			mNrPounceMiss++;

			if (mState.mIsLogging) mState.mLog.log("Pounce: %s MISS\n", mTargetName);
		} else if (result == AttackResult.DODGE) {
			cost = (int) Math.round(cost * 0.2);
			mNrPounceDodge++;

			if (mState.mIsLogging) mState.mLog.log("Pounce: %s DODGE\n", mTargetName);
		} else if (result == AttackResult.PARRY) {
			cost = (int) Math.round(cost * 0.2);
			mNrPounceParry++;

			if (mState.mIsLogging) mState.mLog.log("Pounce: %s PARRY\n", mTargetName);
		} else {
			mNrPounceHit++;
			mMasteryMult = mState.mModel.getBleedMastery();
			mAttackPower = mState.mModel.getAttackPower();
			mPCrit = mState.mModel.getMeleeCritChance();
			mTigersFury = mState.mTigersFuryEv.isUp();
			mToTTMult = mState.mTottEv.getMult();

			if (mState.mIsLogging) mState.mLog.log("Pounce: %s HIT\n", mTargetName);

			schedule(mState.mModel.getPounceDebuffDuration(), mState.mModel.getPounceTickInterval());

			mState.mSimEngine.triggerProc(mTarget, Trigger.YELLOW);

			mState.onCpGenerator(false);

			mTarget.getTargetStunEv().controlledStun(mState.mModel.pounceStunDuration);
		}

		if (mState.mOocEv.isUp()) mNrPounceOoC++;
		mPounceEnergy += mState.onOoCConsumer(cost);

		mState.logResourceChange(prevEnergy, prevCps);

		return 1.0;
	}

	@Override
	public void onTick() {
		AttackResult result = mState.mModel.getDotResult(mPCrit);
		double damage = mState.mModel.levelDep.getPounceDmg() + mAttackPower * mState.mModel.levelDep.getPounceCoeff();

		if (result == AttackResult.CRIT) {
			mNrPounceDotCrit++;
			damage *= mState.mModel.multCrit;
		} else {
			mNrPounceDotHit++;
		}

		damage *= mState.getBleedDamageMult(mMasteryMult, mTargetData);
		damage *= (mTigersFury) ? mState.mModel.multTigersFury : 1.0;
		damage *= mToTTMult;
		if (mTigersFury) mNrPounceDotTFTicks++;
		mPounceDamage += Math.round(damage);

		if (mState.mIsLogging) mState.mLog.log("Pounce Tick: %s %s Damage: %d\n", mTargetName, result, Math.round(damage));

		if (result == AttackResult.CRIT) mState.mSimEngine.triggerProc(mTarget, Trigger.DOTCRIT); else mState.mSimEngine.triggerProc(mTarget, Trigger.DOT);
	}

	@Override
	public void onExpire() {
		if (mState.mIsLogging) mState.mLog.log("Pounce: %s Expiration\n", mTargetName);
	}

	long mPounceDamage = 0;
	int mNrPounceMiss = 0;
	int mNrPounceDodge = 0;
	int mNrPounceParry = 0;
	int mNrPounceHit = 0;
	int mNrPounceDotCrit = 0;
	int mNrPounceDotHit = 0;
	int mNrPounceDotTFTicks = 0;

	int mPounceEnergy;
	int mNrPounceOoC;
}
